package com.coffee.admin.controller.platform;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.controller.BaseController;
import com.coffee.admin.excel.DailiOrderListResultExport;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.PlatformLoginCheck;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.DailiMerchantRoleEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.ExcelUtiles;
import com.coffee.daili.api.query.DailiMerchantQuery;
import com.coffee.daili.api.query.DailiSkuQuery;
import com.coffee.daili.api.req.InsertMerchantReq;
import com.coffee.daili.api.result.*;
import com.coffee.daili.api.service.DailiMerchantService;
import com.coffee.daili.api.service.DailiOrderService;
import com.coffee.daili.api.service.DailiSkuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/platform/daili")
public class PlatformDailiController extends BaseController {
    @DubboReference(check = false)
    private DailiMerchantService dailiMerchantService;
    @DubboReference(check = false)
    private DailiSkuService dailiSkuService;
    @DubboReference(check = false)
    private DailiOrderService dailiOrderService;

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiSkuListAlias")
    @RequestMapping(value = "/merchant/findList", method = {RequestMethod.POST})
    public Result<Page<DailiMerchantResult>> findList(HttpServletRequest request, @RequestBody DailiMerchantQuery params) {
        Result<Page<DailiMerchantResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        params.setPid(0);
        result.setData(dailiMerchantService.getPager(params, pagerInfo));
        return result;
    }

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiSkuOperationAlias")
    @RequestMapping(value = "/merchant/insert", method = {RequestMethod.POST})
    public Result<String> insert(HttpServletRequest request, @RequestBody InsertMerchantReq req) {
        Result<String> result = Result.success(null);
        try {
            LoginUser supplier = getSupplier();
            req.setPlatformId(supplier.getPlatformUserId());
            dailiMerchantService.add(req);
            result.setData("SUCCESS");
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiSkuListAlias")
    @RequestMapping(value = "/merchant/detail", method = {RequestMethod.POST})
    public Result<DailiMerchantResult> detail(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<DailiMerchantResult> result = Result.success(null);
        Integer id = jsonObject.getInteger("id");
        DailiMerchantResult detail = dailiMerchantService.getDetail(id);
        result.setData(detail);
        return result;
    }

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiSkuListAlias")
    @RequestMapping(value = "/fenxiaoyuan/list", method = {RequestMethod.POST})
    public Result<Page<DailiMerchantResult>> fenxiaoyuanDetail(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<Page<DailiMerchantResult>> result = Result.success(null);
        Integer id = jsonObject.getInteger("id");
        String name = jsonObject.getString("name");
        String mobile = jsonObject.getString("mobile");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        DailiMerchantQuery param = new DailiMerchantQuery();
        param.setPid(id);
        param.setRole(DailiMerchantRoleEnum.DISTRIBUTOR.getCode());
        param.setMobile(mobile);
        param.setName(name);
        Page<DailiMerchantResult> pager = dailiMerchantService.getPager(param, pagerInfo);
        result.setData(pager);
        return result;
    }


    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiSkuListAlias")
    @RequestMapping(value = "/sku/list", method = {RequestMethod.POST})
    public Result<Page<DailiSkuResult>> skuList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<Page<DailiSkuResult>> result = Result.success(null);
        Integer categoryId = jsonObject.getInteger("categoryId");
        Integer supplierId = jsonObject.getInteger("supplierId");
        Integer skuId = jsonObject.getInteger("skuId");
        String title = jsonObject.getString("title");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer dailiMerchantId = jsonObject.getInteger("dailiMerchantId");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        DailiSkuQuery param = new DailiSkuQuery();
        param.setCategoryId(categoryId);
        param.setSupplierId(supplierId);
        param.setSkuId(skuId);
        param.setTitle(title);
        param.setDailiMerchantId(dailiMerchantId);
        param.setIsDisable("N");
        param.setIsDelete("N");
        Page<DailiSkuResult> pager = dailiSkuService.getPager(param, pagerInfo);
        result.setData(pager);
        return result;
    }

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiSkuOperationAlias")
    @RequestMapping(value = "/merchant/update", method = {RequestMethod.POST})
    public Result<String> update(HttpServletRequest request, @RequestBody DailiMerchantResult req) {
        Result<String> result = Result.success(null);
        try {
            req.setPlatformId(getSupplier().getPlatformUserId());
            dailiMerchantService.update(req);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiSkuOperationAlias")
    @RequestMapping(value = "/merchant/examine", method = {RequestMethod.POST})
    public Result<String> examine(HttpServletRequest request, @RequestBody DailiMerchantResult req) {
        Result<String> result = Result.success(null);
        try {
            dailiMerchantService.examine(req);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiOrderListAlias")
    @RequestMapping(value = "/order/merchantDetail", method = {RequestMethod.POST})
    public Result<DailiMerchantResult> orderMerchantDetail(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<DailiMerchantResult> result = Result.success(null);
        String date = jsonObject.getString("date");
        Integer id = jsonObject.getInteger("id");
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");
        DailiMerchantResult dailiMerchatResult = dailiOrderService.getDailiMerchatResultByDailiMerchantId(id, date, startTime, endTime);
        result.setData(dailiMerchatResult);
        return result;
    }


    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiOrderListAlias")
    @RequestMapping(value = "/order/fenxiaoyuanList", method = {RequestMethod.POST})
    public Result<Page<FenxiaoYuanList>> orderfenxiaoyuanList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<Page<FenxiaoYuanList>> result = Result.success(null);
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer id = jsonObject.getInteger("id");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<FenxiaoYuanList> fenxiaoYuanList = dailiOrderService.getFenxiaoYuanListByDailiMerchantId(id, null, pagerInfo);
        result.setData(fenxiaoYuanList);
        return result;
    }

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiOrderListAlias")
    @RequestMapping(value = "/order/shanghuList", method = {RequestMethod.POST})
    public Result<Page<ShanghuListResult>> ordershanghuList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<Page<ShanghuListResult>> result = Result.success(null);
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer id = jsonObject.getInteger("id");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<ShanghuListResult> shanghuListResult = dailiOrderService.getShanghuListResultByDailiMerchantId(id, pagerInfo);
        result.setData(shanghuListResult);
        return result;
    }

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiOrderListAlias")
    @RequestMapping(value = "/order/list", method = {RequestMethod.POST})
    public Result<Page<DailiOrderListResult>> orderList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<Page<DailiOrderListResult>> result = Result.success(null);
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String date = jsonObject.getString("date");
        Integer id = jsonObject.getInteger("id");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<DailiOrderListResult> dailiOrderListResult = dailiOrderService.getDailiOrderListResultByDailiMerchantId(id, date,null, null, null, pagerInfo);
        result.setData(dailiOrderListResult);
        return result;
    }

    @OperLog(operModul = "服务商-发薪管理", operDesc = "发薪管理导出")
    @PlatformLoginCheck(alias = "PlatformDailiOrderListAlias")
    @RequestMapping(value = "/export", method = {RequestMethod.POST})
    public void export(HttpServletResponse response, @RequestBody JSONObject jsonObject) {
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");
        Integer id = jsonObject.getInteger("id");
        PagerInfo pagerInfo = setDefaultPageInfo(Integer.MAX_VALUE, 1);
        Page<DailiOrderListResult> dailiOrderListResult = dailiOrderService.getDailiOrderListResultByDailiMerchantId(id, null,"export", startTime, endTime, pagerInfo);
        // 导出Excel文件
        List<DailiOrderListResultExport> excelList = DTOUtils.convertList(dailiOrderListResult.getData(), DailiOrderListResultExport.class);
        ExcelUtiles.exportExcel(excelList, "订单列表", "sheet1", DailiOrderListResultExport.class,
                "dailiOrder.xls", true, response);
    }

    @OperLog(operModul = "平台-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @PlatformLoginCheck(alias = "PlatformDailiOrderListAlias")
    @RequestMapping(value = "/order/merchantList", method = {RequestMethod.POST})
    public Result<Page<HehuorenListResult>> ordermerchantList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<Page<HehuorenListResult>> result = Result.success(null);
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String name = jsonObject.getString("name");
        Date startTime = jsonObject.getDate("startTime");
        Date endTime = jsonObject.getDate("endTime");
        Integer provinceId = jsonObject.getInteger("provinceId");
        Integer cityId = jsonObject.getInteger("cityId");
        Integer areaId = jsonObject.getInteger("areaId");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<HehuorenListResult> hehuorenListResult = dailiOrderService.getHehuorenListResult(name, provinceId, cityId, areaId, pagerInfo,startTime,endTime);
        result.setData(hehuorenListResult);
        return result;
    }
}
